import api from '@/api/cart';
import { Message } from 'element-ui';
const cart = {
    state: {
        myCart: []
    },

    mutations: {
        saveCart: (state, goodsList) => {
            const newData = [];
            goodsList.goodsList.sort((a,b)=> a.goods_status-b.goods_status);//失效的排序
            goodsList.shopList.forEach(shop => {
                newData.push({
                    title: shop.shop_name,
                    shopId: shop.shop_id,
                    shopSelect: false,
                    goods: []
                });
            })
            goodsList.goodsList.forEach(good => {
                newData.forEach((shop,index) => {
                    if(good.shop_id === shop.shopId){
                        let infoTemp = {};
                        goodsList.propertyAndValueList.filter( arr => arr.goods_id ===  good.goods_id).forEach(item=>{
                            infoTemp[`${item.property_name}`] = item.propertyvalue_name;
                        });
                        newData[index].goods.push({
                            id: good.goods_id,
                            name: good.goods_name,
                            imgUrl: good.goods2title_title,
                            info: infoTemp,
                            price: good.goods_price,
                            selected: false,
                            isLossActived: good.goods_status === 2,
                        });
                    }
                })
            })
            state.myCart = [...newData];
        },
    },

    actions: {
        //加入购车
        async fetchAddToCart({ commit },payload) {
            const { code, extend: { message } } = await api.addGoodsToCart(payload);
            if(code === 200){
                Message({
                    showClose: true,
                    message: '已加入购物车!',
                    type: 'success',
                });
                commit('user/saveCartNum',1);
            }else{
                Message({
                    showClose: true,
                    message: message,
                    type: 'error',
                });
            }
        },
        //移出购物车
        async fetchDeleteFromCart({ commit },payload) {
            const { code } = await api.deleteGoodsFromCart(payload);
            if(code === 200){
                Message({
                    showClose: true,
                    message: '该商品已移出购物车!',
                    type: 'success',
                });
                commit('user/saveCartNum',-1);
            }else{
                Message({
                    showClose: true,
                    message: '移出失败,请重试!',
                    type: 'error',
                });
            }
        },
        //批量删除deleteMoreGoodsFromCart
        async fetchDeleteMore({ commit },payload) {
            const { code } = await api.deleteMoreGoodsFromCart(payload);
            if(code === 200){
                Message({
                    showClose: true,
                    message: '该商品已移出购物车!',
                    type: 'success',
                });
                commit('user/saveCartNum',-(payload.goods_idList.length));
            }else{
                Message({
                    showClose: true,
                    message: '移出失败,请重试!',
                    type: 'error',
                });
            }
        },
        //购物车接口
        async fetchCartInfo({ commit },payload) {
            const { code, extend:{ result } } = await api.getGoodsInfoFromCart(payload);
            if(code === 200){
                commit('saveCart',result.goodsList)
            }
        },
        //清楚失效
        async fetchClearFailedGoods({ commit },payload) {
            const { code, extend:{ result } } = await api.clearFailedGoods(payload);
            if(code === 200){
                Message({
                    showClose: true,
                    message: '已清除失效!',
                    type: 'success',
                });
                commit('user/saveCartNum', -payload.goods_idList.length);
            }else{
                Message({
                    showClose: true,
                    message: '清除失败,请重试!',
                    type: 'error',
                });
            }
        },
        //加入收藏夹
        async fetchAddToCollect({ commit },payload) {
            const { code } = await api.addAllGoodsToCollect(payload);
            if(code === 200){
                Message({
                    showClose: true,
                    message: '收藏成功!',
                    type: 'success',
                });
                commit('user/saveCartNum', -payload.goods_idList.length)
            }else{
                Message({
                    showClose: true,
                    message: '收藏失败,请重试!',
                    type: 'error',
                });
            }
        },
    }
}

export default cart